package cz.muni.fi.pa165.languageschool.service.impl;

import cz.muni.fi.pa165.languageschool.Lecturer;
import cz.muni.fi.pa165.languageschool.Student;
import cz.muni.fi.pa165.languageschool.utils.Authority;
import java.util.ArrayList;
import java.util.Collection;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * Student,Lecturer converter into spring User class
 *
 * @author Milos, Marketa Trachtova (255498) marketa.tr(at)gmail.com
 */
@Service("assembler")
public class Assembler {

    final static Logger log = LoggerFactory.getLogger(Assembler.class);

    public Assembler() {
    }

    /**
     * Transforms student into spring User
     *
     * @param student
     * @return user details
     */
    @Transactional(readOnly = true)
    public UserDetails tranformStudentToUser(Student student) {
        log.info("***** UTILS Assembler transformStudentToUser: {}", student);
        String username = student.getUsername();
        String password = student.getPassword();
        boolean enabled = student.isEnabled();
        boolean accountNonExpired = student.isEnabled();
        boolean credentialsNonExpired = student.isEnabled();
        boolean accountNonLocked = student.isEnabled();
        Authority authority = student.getAuthority();

        Collection<GrantedAuthority> authorities = new ArrayList<>();
        GrantedAuthority grantedAuthority = new SimpleGrantedAuthority(authority.toString());
        authorities.add(grantedAuthority);

        User user = new User(username, password, enabled, accountNonExpired, credentialsNonExpired, accountNonLocked, authorities);
        log.debug("*** UTILS Assembler STUDENT assembled into user= {}", user);
        return user;
    }

    /**
     * Transforms lecturer into spring User
     *
     * @param lecturer
     * @return user details
     */
    @Transactional(readOnly = true)
    public UserDetails tranformLecturerToUser(Lecturer lecturer) {
        log.info("***** UTILS Assembler transformLecturerToUser: {}", lecturer);
        String username = lecturer.getUsername();
        String password = lecturer.getPassword();
        boolean enabled = lecturer.isEnabled();
        boolean accountNonExpired = lecturer.isEnabled();
        boolean credentialsNonExpired = lecturer.isEnabled();
        boolean accountNonLocked = lecturer.isEnabled();
        Authority authority = lecturer.getAuthority();

        Collection<GrantedAuthority> authorities = new ArrayList<>();
        GrantedAuthority grantedAuthority = new SimpleGrantedAuthority(authority.toString());
        authorities.add(grantedAuthority);

        User user = new User(username, password, enabled, accountNonExpired, credentialsNonExpired, accountNonLocked, authorities);
        log.debug("*** UTILS Assembler LECTURER assembled into user= {}", user);
        return user;
    }
}
